﻿using Assets.Scripts.Manager;
using System;
using System.Collections;
using System.Diagnostics.Contracts;
using Unity.VisualScripting;
using UnityEngine;
using UnityEngine.SceneManagement;
using moai_kun_v1;
public enum GameState 
{
    None,
    Start,
    Reset,
    Pause,
    Running,
    Failure,
    End,
}

public class GameManager : MonoBehaviour
{
    public static GameManager Instance;

    private void Awake()
    {
        if (Instance != null && Instance != this)
        {
            Destroy(gameObject);
            return;

        }
        Instance = this;
        DontDestroyOnLoad(gameObject);
    }

    private GameState _currentState=GameState.None;

    private Action<GameState> OnGameStateUpdate;

    public void UpdateGameState(GameState state) 
    {
        if (state == _currentState) 
        {
            return;
        }
        switch (state)
        {
            case GameState.Start:
                StartGame();
                break;
            case GameState.Reset:
                ResetLevel();
                break;
            case GameState.Running:
                Running();
                break;
            case GameState.Pause:
                Pause(true);
                break;
            case GameState.Failure:
                Failure();
                break;
            case GameState.End:
                GameOver();
                break;
            default:
                break;
        }
        _currentState = state;
        OnGameStateUpdate?.Invoke(state);
    }

  
    private MainPlayer _currentPlayer;
    public MainPlayer CurrentPlayer => _currentPlayer;

    public int CurrentStageIndex => _stageIndex;

    private int _resetCount=3;
    private int _stageIndex=1;

    private void StartGame()
    {
        SceneManager.LoadScene(0);
        UIManager.Instance.Show(GameState.Start);
    }

    public void LoadDefaultLevel() 
    {
        _resetCount = 3;
        _stageIndex = 1;
        ResetLevel();
    }
    /// <summary>
    /// 重置当前关卡
    /// </summary>
    public void ResetLevel()
    {
        _currentState = GameState.Reset;
        var obj=UIManager.Instance.Show(GameState.Reset);
        var ui=obj.GetComponent<CrossStageView>();
        ui.Load(_stageIndex,_resetCount);
    }
    public void LoadScene()
    {
        StartCoroutine(LoadScenAsync());
    }

    private IEnumerator LoadScenAsync()
    {
        yield return SceneManager.LoadSceneAsync(_stageIndex);
        Running();
    }

    private void Running()
    {
        
        AudioManager.Instance.PlayBGM(_stageIndex,true);
        //LevelManager.Instance.ResetLevel();
        _currentPlayer = GameObject.FindObjectOfType<MainPlayer>();
        _currentPlayer.Reset();
        UIManager.Instance.Show(GameState.Running);
        TimeManager.Instance.StartTime();
    }

    public void Failure()
    {
        AudioManager.Instance.PlaySound(SoundType.Die);
        _currentState= GameState.Failure;
        _resetCount--;
        Debug.Log(_resetCount);
        if (_resetCount < 0) 
        {
            GameOver();
        }
        else 
        {
            ResetLevel();
        }
    }

    public void GameOver()
    {
        AudioManager.Instance.PlaySound(SoundType.GameOver);
        var ui = UIManager.Instance.Show(GameState.Failure);
        DeathView obj = ui.GetComponent<DeathView>();
        obj.LoadGameOver();
    }

    public void IncreaseLevel()
    {
        _stageIndex++;
        _resetCount++;
    }

    public void Pause(bool doing)
    {
        AudioManager.Instance.PlaySound(SoundType.Select);
        if (doing) 
        {
            Time.timeScale = 0;
        }
        else 
        {
            Time.timeScale = 1;
        }
    }
}
